package com.musiklegal;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class Voucher extends Activity {
	String temp= "";
	Calendar ci = Calendar.getInstance();
	 String tid =""+ci.get(Calendar.YEAR) + "-" + 
    (ci.get(Calendar.MONTH) + 1) + "-" +
    ci.get(Calendar.DAY_OF_MONTH) + " " +
    ci.get(Calendar.HOUR) + ":" +
    ci.get(Calendar.MINUTE) +  ":" +
    ci.get(Calendar.SECOND);
	 public void onCreate(Bundle savedInstanceState) 
	    {
		 if(!MusikLegal.internet(getApplicationContext()))
			 finish();
		 
	        super.onCreate(savedInstanceState);
	        setContentView(R.layout.voch);
	        Button go = (Button) this.findViewById(R.id.go);
	        go.setOnClickListener(new OnClickListener() {
				
				public void onClick(View arg0) {
					tryTop();
				
				}

			});
	    }
	 
	 public void onResume()
		{
			if(!MusikLegal.internet(getApplicationContext()))
			{
				
				Toast.makeText(getApplicationContext(), "You are unable to connect", Toast.LENGTH_SHORT).show();
				finish();
			}
			super.onResume();
		}
	 
	 void tryTop()
	    {
	    	
	    	EditText etxt_vouch = (EditText) findViewById(R.id.voucher);
			
	    	String username = MusikLegal.mPreferences.getString("UserName", "default");
			String id = tid;
	    	String vouch = etxt_vouch.getText().toString();
	    	
	    	DefaultHttpClient client = new DefaultHttpClient();
	        HttpPost httppost = new HttpPost("http://topup.musiklegal.com/webservice.php");
	        List< BasicNameValuePair > nvps = new ArrayList< BasicNameValuePair >();
	        nvps.add(new BasicNameValuePair("username", username));
	        nvps.add(new BasicNameValuePair("cid", id));
	        nvps.add(new BasicNameValuePair("voucher", vouch));
	 
	        try {   
	      	  	UrlEncodedFormEntity p_entity = new UrlEncodedFormEntity(nvps,HTTP.UTF_8);
	            
	            httppost.setEntity(p_entity);
	            
	            HttpResponse response = client.execute(httppost);
	            HttpEntity responseEntity = response.getEntity();
	            

	            SAXParserFactory spf = SAXParserFactory.newInstance();
	            SAXParser sp = spf.newSAXParser();
	            XMLReader xr = sp.getXMLReader();
	            
	            LoginHandler myLoginHandler = new LoginHandler();
	            xr.setContentHandler(myLoginHandler);
	            xr.parse(retrieveInputStream(responseEntity));
	            ParsedLoginDataSet parsedLoginDataSet = myLoginHandler.getParsedLoginData();
	            temp = parsedLoginDataSet.getExtractedString();

	            
	            if (parsedLoginDataSet.getExtractedString().equals("SUCCESS")) 
	            {
	            	AlertDialog alertDialog = new AlertDialog.Builder(this).create();
        			alertDialog.setTitle("Success");
        			alertDialog.setMessage("Top up by hardvoucher success");
        			alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
        			   public void onClick(DialogInterface dialog, int which) {
        				   startActivity(new Intent(getApplicationContext(), MainMenu.class));
        				   finish();
        			   }
        			});
        			alertDialog.setIcon(R.drawable.oc2);
        			alertDialog.show();
	            	
	            } else 
	            	{	
	        			AlertDialog alertDialog = new AlertDialog.Builder(this).create();
	        			alertDialog.setTitle("Wrong voucher code");
	        			alertDialog.setMessage("You Enter Wrong voucher code");
	        			alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
	        			   public void onClick(DialogInterface dialog, int which) {
	        				   finish();
	        			   }
	        			});
	        			alertDialog.setIcon(R.drawable.oc2);
	        			alertDialog.show();
	        	}
	                 
	                   
	            
	      } catch (Exception e)
	      {
	          
	      }
	    
	    
	    
	    }
	 private InputSource retrieveInputStream(HttpEntity httpEntity) {
	        InputSource insrc = null;
	        try {
	              insrc = new InputSource(httpEntity.getContent());
	        } catch (Exception e) {
	        }
	        return insrc;
	  } 
	   
}
